home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 2.7 KB | 110 lines |
- // NcgiServer - an NCGI server
- //
- // Copyright (c) 1996 Sun Microsystems, Inc. All Rights reserved
- // Permission to use, copy, modify, and distribute this software
- // and its documentation for NON-COMMERCIAL purposes and without
- // fee is hereby granted provided that this copyright notice
- // appears in all copies. Please refer to the file copyright.html
- // for further important copyright and licensing information.
- //
- // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
-
- package sun.servlet.apache;
-
- import java.io.*;
- import java.net.*;
-
- /** An NCGI server.
- */
-
- public class NcgiServer
- {
-
- private ServerSocket serverSocket;
-
- /** Create an NCGI server.
- ** @exception NcgiException if there's a problem
- */
- public NcgiServer( int port, String authfile ) throws NcgiException
- {
- try
- {
- serverSocket = new ServerSocket( port, Integer.MAX_VALUE );
- }
- catch ( IOException e )
- {
- throw new NcgiException(
- "problem creating server socket: " + e.toString() );
- }
- makeAuthfile( authfile );
- }
-
-
- public Socket accept() throws NcgiException
- {
- try
- {
- Socket socket = serverSocket.accept();
- return socket;
- }
- catch ( IOException e )
- {
- throw new NcgiException(
- "problem accepting new socket: " + e.toString() );
- }
- }
-
-
- public void done()
- {
- nukeAuthfile();
- try
- {
- serverSocket.close();
- }
- catch ( IOException e ) {}
- }
-
-
- private String authfile;
- private File authFile;
- protected byte[] authBytes = new byte[25]; // arbitrary size
-
- /** Initialize the authorization file.
- ** @param authfile the name of the file with the authorization string
- ** @exception NcgiException if there's a problem
- */
- private void makeAuthfile( String authfile ) throws NcgiException
- {
- this.authfile = authfile;
- for ( int i = 0; i < authBytes.length; ++i )
- authBytes[i] = (byte) ( Math.random() * 256.0 );
- try
- {
- authFile = new File( authfile );
- OutputStream out = new FileOutputStream( authFile );
- out.write( authBytes );
- out.close();
- }
- catch ( IOException e )
- {
- throw new NcgiException(
- "problem making authfile: " + e.toString() );
- }
- }
-
- /** Get rid of the authorization file. Can be called when your server
- ** is exiting, if you're paranoid.
- */
- private void nukeAuthfile()
- {
- authFile.delete();
- }
-
- }
-